/**
  **************************************************************************
  * @file    clientconfig.c
  * @author  huhao
  * @version V1.0.0
  * @date    10-20-2020
  * @brief   读取配置文件信息
  * @project linux client
  ***************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2020 上海今中网络科技有限公司 </center></h2>
  ***************************************************************************
*/
#include    "log-util.h"                //这种依赖竟然会改变对应的执行顺序!!!
#include    "clientconfig.h"
#include    "config-util.h"

#ifndef     CLIENT_CONFIG_PATH
#define     CLIENT_CONFIG_PATH      "./config/secureClient.conf"
#endif

#ifndef     CLIENT_CERT_PATH
#define     CLIENT_CERT_PATH        "./certs/linuxClient.cer"
#endif

//global config
CONFIG_UTIL_T configUtil;

/**
  * @brief      初始化配置
  * @param      CLIENT_CONFIG_T *
  * @retval     完成客户端配置对象初始化
  */
void CLIENT_CONFIG_INIT(CLIENT_CONFIG_T *clientConfig)
{
    //init util class
    CONFIG_UTIL_INIT(&configUtil);

    assert_param( RET_SUCCESS == configUtil.readConfig(&configUtil, CLIENT_CONFIG_PATH) );        //?suitable here?

    clientConfig->serverIp      =   configUtil.getString( &configUtil, "serverIp", "192.168.1.11"  );
    clientConfig->serverPort    =   configUtil.getInt   ( &configUtil, "serverPort", 10000         );
    clientConfig->certPath      =   configUtil.getString( &configUtil, "certPath", CLIENT_CERT_PATH);
    clientConfig->vpnConfigPath =   configUtil.getString( &configUtil, "vpnConfigPath", "");
    clientConfig->vpnBin        =   configUtil.getString( &configUtil, "vpnBin", "");

    CONFIG_UTIL_FREE(&configUtil);
}


/**
  * @brief      释放配置
  * @param      CLIENT_CONFIG_T *
  * @retval     完成客户端配置对象销毁
  */
void CLIENT_CONFIG_FREE(CLIENT_CONFIG_T *clientConfig)
{
    clientConfig->serverPort = 0;
    if(clientConfig->serverIp != NULL)
        free( clientConfig->serverIp );
    if(clientConfig->certPath != NULL)
        free( clientConfig->certPath );
    if(clientConfig->vpnConfigPath != NULL)
        free( clientConfig->vpnConfigPath );
    if(clientConfig->vpnBin != NULL)
        free( clientConfig->vpnBin );
}
